
var bgPage = chrome.extension.getBackgroundPage();
$(function(){
	//载入上次请求的数据 以保证界面美观
	if(bgPage.oscapi.xml){
		xml2dom("#all .list",bgPage.oscapi.xml[0]);
		xml2dom("#atme .list",bgPage.oscapi.xml[1]);
		xml2dom("#review .list",bgPage.oscapi.xml[2]);
		Tweet2dom("#Tweet .list",bgPage.oscapi.xml[3]);
	}
	if (bgPage.oscapi.notice){
		printNotice(bgPage.oscapi.notice);
	}
	asynchronous(getAll);
	$(".dongtanform button").click(function(){
		var mag = $(".dongtanform textarea").val();
		if(bgPage.oscapi.pubTweet(mag)){
			$(".dongtanform textarea").val("");
		}else{

		}
	})
})
$('.tab').Tabs({
    event:'click',
    callback:tabcallback
});

function tabcallback(){
	var n = $(this).find(".tab_menu li").index($(this).find(".tab_menu li.current"));
	$(".UpPrompt").addClass("show");
	var upend = function(){
		$(".UpPrompt").removeClass("show");
	}
	switch(n)
	{
	case 0:
	  asynchronous(getTweetList,upend);
	  break;
	case 1:
	  asynchronous(getAll,upend);
	  break;
	case 2:
	  asynchronous(getAtme,upend);
	  break;
	case 3:
	  asynchronous(getReview,upend);
	  break;
	case 4:
	  asynchronous(getMessageList,upend);
	  break;
	}
	console.log(n);
}
var liststring = hereDoc(function(){
/*<li>
	<div class="img">
		<a href="{0}" target="_blank"><img src="{1}" alt="{2}"></a>
	</div>
	<div class="show">
		<a href="{3}" target="_blank">{4}</a>
	</div>
</li>*/
});


/*创建模板*/
var listT = new Template(liststring);
function getAll(){
	var xml = bgPage.oscapi.getActiveList(1);
	bgPage.oscapi.xml[0] = xml;
	console.log(xml);
    xml2dom("#all .list",xml);
}
function getAtme(){
	var xml = bgPage.oscapi.getActiveList(2);
	bgPage.oscapi.xml[1] = xml;
    xml2dom("#atme .list",xml);
}
function getReview(){
	var xml = bgPage.oscapi.getActiveList(3);
	bgPage.oscapi.xml[2] = xml;
	console.log(xml);
	xml2dom("#review .list",xml);
}
function getTweetList(){
	var xml = bgPage.oscapi.getTweetList();
	bgPage.oscapi.xml[3] = xml;
	Tweet2dom("#Tweet .list",xml);
}
function getMessageList(){
	var xml = bgPage.oscapi.getMessageList();
	bgPage.oscapi.xml[4] = xml;
	Message2dom("#Message .list",xml);
}
function xml2dom(id,xml){
	$(id).empty();
	var $list = $(xml).find("activies active");
	for (var i=0;i<$list.length;i++)
    {
    	var xmlarr = bgPage.xml2arr($list[i]);
    	if(!xmlarr.portrait){xmlarr.portrait = DEFAULT_PICTURES}else{xmlarr.portrait.replace("_50","_100")};
      	var homeurl = "http://my.oschina.net/u/"+xmlarr.authorid;
    	var pageurl = "http://my.oschina.net/u/"+xmlarr.authorid+"/tweet/"+xmlarr.objectID;

    	var text = listT.format(homeurl,xmlarr.portrait,xmlarr.author,pageurl,xmlarr.message);
    	$(id).append(text);
    }
}

function Tweet2dom($id,xml){
	$($id).empty();
	var $list = $(xml).find("tweets tweet");
	for (var i=0;i<$list.length;i++)
    {
    	var xmlarr = bgPage.xml2arr($list[i]);
    	if(!xmlarr.portrait){xmlarr.portrait = DEFAULT_PICTURES}else{xmlarr.portrait.replace("_50","_100")};
      	var homeurl = "http://my.oschina.net/u/"+xmlarr.authorid;
    	var pageurl = "http://my.oschina.net/u/"+xmlarr.authorid+"/tweet/"+xmlarr.id;
    	var text = listT.format(homeurl,xmlarr.portrait,xmlarr.author,pageurl,xmlarr.body);
    	$($id).append(text);
    }
}

function Message2dom($id,xml){
	$($id).empty();
	var $list = $(xml).find("messages message");
	for (var i=0;i<$list.length;i++)
    {
    	var xmlarr = bgPage.xml2arr($list[i]);
    	if(!xmlarr.portrait){xmlarr.portrait = DEFAULT_PICTURES}else{xmlarr.portrait.replace("_50","_100")};
    	// console.log(xmlarr);
	  	var portrait = ($($list[i]).find("portrait").text()).replace("_50","_100");
	  	if(!portrait){portrait = DEFAULT_PICTURES};
	    var homeurl = "http://my.oschina.net/u/"+xmlarr.friendid;
	  	var pageurl = "http://my.oschina.net/xioxin/admin/msgs?user="+xmlarr.friendid;
		var text = listT.format(homeurl,portrait,xmlarr.friendname,pageurl,xmlarr.content);
    	$($id).append(text);
    }
}







function printNotice(array){
	$(".tab_menu li").eq(2).find("b").text(array.atmeCount!=0?array.atmeCount:'');
	$(".tab_menu li").eq(3).find("b").text(array.reviewCount!=0?array.reviewCount:'');
	$(".tab_menu li").eq(4).find("b").text(array.msgCount!=0?array.msgCount:'');
}
